/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QuestionGenerator;

import java.io.File;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Raito
 */
public class QAGenerator {

    public List<TypeQuestion> initData() {
        List<TypeQuestion> typeQuestion = new ArrayList<TypeQuestion>();
        try {

            File file = new File("Configuration/sentence.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("sentence");
            for (int s = 0; s < nodeList.getLength(); s++) {

                Element fstNode = (Element) nodeList.item(s);
                String type = fstNode.getAttribute("dang");
                String sentence = fstNode.getAttribute("cau");
                List<String> patterns = new ArrayList<String>();
                NodeList nodeChilds = fstNode.getElementsByTagName("item");
                for (int i = 0; i < nodeChilds.getLength(); i++) {
                    Element nodeChild = (Element) nodeChilds.item(i);
                    patterns.add(nodeChild.getTextContent());
                }
                TypeQuestion question = new TypeQuestion();
                question.setPattern(patterns);
                question.setSentence(sentence);
                question.setType(type);
                typeQuestion.add(question);

            }
            return typeQuestion;

        } catch (Exception ex) {
        }
        return null;



    }

    public List<String> readXmlFile(String fileName) {
        List<String> data = new ArrayList<String>();
        try {
            File file = new File("Vocabulary/Data/" + fileName + ".xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("item");
            for (int s = 0; s < nodeList.getLength(); s++) {

                Element fstNode = (Element) nodeList.item(s);
                String nodeValue = fstNode.getTextContent();
                data.add(nodeValue);
            }
            return data;

        } catch (Exception ex) {
        }
        return null;

    }

    public List<String> initQuestion(List<String> patterns) {
        List<String> questions = new ArrayList<String>();
        List<List> patternData;
        for (String pattern : patterns) {
            patternData = new ArrayList<List>();
            String[] split = pattern.split(" ");
            for (String s : split) {
                if (s.contains("{")) {
                        List<String> data = readXmlFile(s.substring(1, s.length() - 1));
                        patternData.add(data);
                } else {
                    List<String> data = new ArrayList<String>();
                    data.add(s);
                    if(s.equals("gì"))
                    {
                        data.add("nào");
                    }
                    else if(s.equals("loài"))
                    {
                        data.add("con");
                        data.add("con vật");
                    }
                    patternData.add(data);
                }
            }
            List<String> generateQuestionByPattern = generateQuestionByPattern(patternData);
            for(String question: generateQuestionByPattern)
            {
                questions.add(question);
            }
        }

        return questions;

    }

    private List<String> generateQuestionByPattern(List<List> patternData) {
        List<String> listQuestion = new ArrayList<String>();
        List<String> tempQuestion;

        for (List<String> data : patternData) {
            if (listQuestion.isEmpty()) {
                listQuestion = data;
            } else {
                tempQuestion = new ArrayList<String>();
                for (String character : data) {
                    for (String question : listQuestion) {
                        tempQuestion.add(question.replace("_", " ") + " " + character.replace("_", " "));
                    }
                }
                listQuestion = tempQuestion;
            }
        }


        return listQuestion;
    }
}
